package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.progress.core.application.command.MessageSendCommand;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.MessageRemoteService;
import com.smedi.ismedi.notification.api.dto.request.SendSmsRequest;
import com.smedi.ismedi.notification.api.feign.RemoteSmsNotificationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class MessageRemoteServiceImpl implements MessageRemoteService {

    private final RemoteSmsNotificationFeignApi remoteSmsNotificationFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public void sendMessage(MessageSendCommand messageSendCommand) {
        try {
            SendSmsRequest sendSmsRequest = adapterConverter.sendMessageCommandToSendSmsRequest(messageSendCommand);
            ResponseData<Boolean> postSendShortMessage = remoteSmsNotificationFeignApi.postSendShortMessage(sendSmsRequest);
            if (!postSendShortMessage.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, postSendShortMessage.getApplication(), String.valueOf(postSendShortMessage.getErrorMsg()));
            }
        } catch (Exception ignore) {
        }
    }
}
